Skip to content

fix: prevent OCR thread cleanup crash - #108

Open
dengzhongyuan365-dev wants to merge 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:fix/ocr-thread-cleanup
Open

dengzhongyuan365-dev wants to merge 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:fix/ocr-thread-cleanup

Conversation

@dengzhongyuan365-dev

@dengzhongyuan365-dev dengzhongyuan365-dev commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

变更说明

修复 deepin-ocr 识别线程结束时可能出现的 SIGSEGV。

  • 将 QThread::finished 清理显式投递回 MainWidget 所在线程。
  • 在线程清理完成前禁止复用同一个 QThread 对象。
  • 析构时等待线程结束;超时后调用 terminate() 并再次 wait() 后才删除线程对象。
  • 将跨线程读取的 m_isEndThread 改为 std::atomic_bool。

根因

原实现通过无 context 的 lambda 连接 QThread::finished,lambda 实际运行在 OCR 工作线程,并直接访问、清空、删除 MainWidget::m_loadImagethread。窗口关闭或重新识别时,该操作可能与主线程清理并发,对空指针或悬空线程对象调用 deleteLater(),最终在 QObject::deleteLater() 中崩溃。SW64 上 OCR 推理较慢,更容易触发。

验证计划

  • 在 SW64 上重复执行截图工具栏进入 OCR 的流程。
  • OCR 识别过程中关闭窗口,确认进程不再崩溃。
  • OCR 识别过程中切换语言触发重新识别,确认线程生命周期正常。
  • 在 x86 上执行相同流程验证无回归。

按用户要求,本次未在本机编译。

Multica: V-9975

Summary by Sourcery

Make OCR thread cleanup safe during shutdown and repeated recognition.

Bug Fixes:

  • Prevent OCR thread cleanup from racing with the main widget and crashing during window closure or recognition restarts.

Enhancements:

  • Defer a requested rerun until the previous OCR thread has finished cleanup, and synchronize thread-state flags across threads.
  • Wait for OCR thread termination before deleting it during widget destruction, including after a timeout.

1. Marshal QThread finished cleanup back to MainWidget's thread.
2. Prevent reuse of a thread before its cleanup has completed.
3. Wait for a terminated OCR thread before deleting its object.

Log: Prevent OCR worker thread cleanup crashes.

Influence:
1. Close the OCR window while recognition is running and verify no crash.
2. Trigger repeated recognition and verify each OCR thread exits cleanly.
3. Repeat the screenshot-to-OCR flow on SW64 and x86.

fix: 修复 OCR 识别线程清理竞态

1. 将 QThread finished 清理显式投递回 MainWidget 所在线程。
2. 避免在线程清理完成前复用同一个线程对象。
3. 终止 OCR 线程后等待其真正退出再删除对象。

Log: 修复 OCR 识别线程退出时的清理崩溃。

Influence:
1. OCR 识别过程中关闭窗口,验证不会崩溃。
2. 重复触发识别,验证每次 OCR 线程均可正常退出。
3. 在 SW64 和 x86 上重复截图进入 OCR 的流程。

Log: Prevent OCR worker thread cleanup crashes.
Issue: V-9975
multica: V-9975
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Dec 2, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Make destruction stop and join the OCR thread before deleting it, including after a timeout and forced termination.

  • src/mainwidget.cpp L55-70

Coordinate OCR thread creation, completion cleanup, reruns, and the atomic end-state across the widget and worker thread.

  • src/mainwidget.cpp L355 (removed lines), L497-498, L511-530
  • src/mainwidget.h L20-21, L118

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/mainwidget.cpp" line_range="528" />
<code_context>
         deleteLoadingUi();
-        if(m_needReRunRec) {
-            m_needReRunRec = false;
-            runRec(false);
-        }
     });
</code_context>
<issue_to_address>
**New image shows stale OCR result**

When a different image is opened in the same widget while OCR is still running, `runRec(true)` returns before `OCREngine::setImage()` and records only a boolean rerun request; the finished callback calls `runRec(false)`, so the engine recognizes its previous image and displays that result for the new image.

Have the finished callback rerun OCR in a way that sets the engine's image to the newly opened image before recognition.

Also at `src/mainwidget.cpp:497-499`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/mainwidget.cpp
thread->deleteLater();

if (m_needReRunRec.exchange(false))
runRec(false);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · New image shows stale OCR result

When a different image is opened in the same widget while OCR is still running, runRec(true) returns before OCREngine::setImage() and records only a boolean rerun request; the finished callback calls runRec(false), so the engine recognizes its previous image and displays that result for the new image.

Have the finished callback rerun OCR in a way that sets the engine's image to the newly opened image before recognition.

Also at src/mainwidget.cpp:497-499.

Prompt for AI agents
In `src/mainwidget.cpp` at line 528:

**New image shows stale OCR result**

When a different image is opened in the same widget while OCR is still running, `runRec(true)` returns before `OCREngine::setImage()` and records only a boolean rerun request; the finished callback calls `runRec(false)`, so the engine recognizes its previous image and displays that result for the new image.

Have the finished callback rerun OCR in a way that sets the engine's image to the newly opened image before recognition.

Also at `src/mainwidget.cpp:497-499`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants